package com.ruoyi.monitor.infoprojectmonitor.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.monitor.infoprojectmonitor.domain.InfoProjectMonitor;
import com.ruoyi.monitor.infoprojectmonitor.mapper.InfoProjectMonitorMapper;
import com.ruoyi.monitor.infoprojectmonitor.service.IInfoProjectMonitorService;
import com.ruoyi.monitor.infoprojectmonitor.vo.ProjectMonitorVo;
import com.ruoyi.monitor.infoprojectmonitorfunds.domain.InfoProjectMonitorFunds;
import com.ruoyi.monitor.infoprojectmonitorfunds.mapper.InfoProjectMonitorFundsMapper;
import com.ruoyi.monitor.infoprojectmonitorindex.domain.InfoProjectMonitorIndex;
import com.ruoyi.monitor.infoprojectmonitorindex.mapper.InfoProjectMonitorIndexMapper;
import com.ruoyi.monitor.infoprojectmonitorrelation.domain.InfoProjectMonitorRelation;
import com.ruoyi.monitor.infoprojectmonitorrelation.mapper.InfoProjectMonitorRelationMapper;
import com.ruoyi.monitor.infoprojectmonitorrelation.vo.InfoProjectMonitorDetailVo;
import com.ruoyi.project.infoprojecttargetindex.domain.InfoProjectTargetIndex;
import com.ruoyi.project.infoprojecttargetindex.mapper.InfoProjectTargetIndexMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 项目监控Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-14
 */
@Service
public class InfoProjectMonitorServiceImpl implements IInfoProjectMonitorService {
    @Resource
    private InfoProjectMonitorMapper infoProjectMonitorMapper;
    @Resource
    private InfoProjectMonitorFundsMapper infoProjectMonitorFundsMapper;
    @Resource
    private InfoProjectTargetIndexMapper infoProjectTargetIndexMapper;
    @Resource
    private InfoProjectMonitorIndexMapper infoProjectMonitorIndexMapper;

    @Resource
    private InfoProjectMonitorRelationMapper infoProjectMonitorRelationMapper;

    /**
     * 查询项目监控
     *
     * @param id 项目监控主键
     * @return 项目监控
     */
    @Override
    public InfoProjectMonitor selectInfoProjectMonitorById(Long id) {
        return infoProjectMonitorMapper.selectInfoProjectMonitorById(id);
    }

    /**
     * 查询项目监控列表
     *
     * @param infoProjectMonitor 项目监控
     * @return 项目监控
     */
    @Override
    public List<InfoProjectMonitor> selectInfoProjectMonitorList(InfoProjectMonitor infoProjectMonitor) {
        return infoProjectMonitorMapper.selectInfoProjectMonitorList(infoProjectMonitor);
    }

    /**
     * 新增项目监控
     *
     * @param infoProjectMonitor 项目监控
     * @return 结果
     */
    @Override
    public int insertInfoProjectMonitor(InfoProjectMonitor infoProjectMonitor) {
        infoProjectMonitor.setCreateTime(DateUtils.getNowDate());
        return infoProjectMonitorMapper.insertInfoProjectMonitor(infoProjectMonitor);
    }

    /**
     * 修改项目监控
     *
     * @param infoProjectMonitor 项目监控
     * @return 结果
     */
    @Override
    public int updateInfoProjectMonitor(InfoProjectMonitor infoProjectMonitor) {
        infoProjectMonitor.setUpdateTime(DateUtils.getNowDate());
        return infoProjectMonitorMapper.updateInfoProjectMonitor(infoProjectMonitor);
    }

    /**
     * 批量删除项目监控
     *
     * @param ids 需要删除的项目监控主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectMonitorByIds(Long[] ids) {
        return infoProjectMonitorMapper.deleteInfoProjectMonitorByIds(ids);
    }

    /**
     * 删除项目监控信息
     *
     * @param id 项目监控主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectMonitorById(Long id) {
        return infoProjectMonitorMapper.deleteInfoProjectMonitorById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int reportProjectMonitor(ProjectMonitorVo projectMonitorVo) {

        InfoProjectMonitor infoProjectMonitor = infoProjectMonitorMapper.selectInfoProjectMonitorById(projectMonitorVo.getProjectId());
        //1.修改状态为待填报
        InfoProjectMonitor infoProjectMon=new InfoProjectMonitor();
        infoProjectMon.setId(infoProjectMonitor.getId());
        infoProjectMon.setExamineStatus(1);
        infoProjectMonitorMapper.updateInfoProjectMonitor(infoProjectMon);
        if (infoProjectMonitor == null) {
            throw new RuntimeException("项目不存在");
        }
        //2.向关联表中先插入一条数据
        InfoProjectMonitorRelation infoProjectMonitorRelation = new InfoProjectMonitorRelation();
        infoProjectMonitorRelation.setProjectId(projectMonitorVo.getProjectId());
        infoProjectMonitorRelation.setMonitorTime(projectMonitorVo.getMonitorTime());
        infoProjectMonitorRelation.setRemark(projectMonitorVo.getRemark());//下达意见
        infoProjectMonitorRelation.setCreateTime(new Date());
        infoProjectMonitorRelation.setUpdateTime(new Date());//修改时间
        infoProjectMonitorRelation.setMonitorStatus(1);//默认待填报
        infoProjectMonitorRelation.setCreateBy(SecurityUtils.getLoginUser().getUser().getDept().getDeptName() + "-" + SecurityUtils.getLoginUser().getUser().getNickName());
        int i1 = infoProjectMonitorRelationMapper.insertInfoProjectMonitorRelation(infoProjectMonitorRelation);
        //3.资金监控表中增加该条记录
        InfoProjectMonitorFunds infoProjectMonitorFunds = new InfoProjectMonitorFunds();
        BeanUtils.copyProperties(infoProjectMonitor, infoProjectMonitorFunds);
        infoProjectMonitorFunds.setProjectMonitorRelationId(infoProjectMonitorRelation.getId());
        infoProjectMonitorFunds.setCreateTime(new Date());
        infoProjectMonitorFunds.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        int i = infoProjectMonitorFundsMapper.insertInfoProjectMonitorFunds(infoProjectMonitorFunds);
        //3.项目监控指标表中增加数据
        //如果向目标库中的 infoProjectMonitor.getProjectTargetId() 为空不执行
        if (infoProjectMonitor.getProjectTargetId() != null) {
            InfoProjectTargetIndex infoProjectTargetIndex = new InfoProjectTargetIndex();
            infoProjectTargetIndex.setProjectId(infoProjectMonitor.getProjectTargetId());
            //3.1查询目标填报中的指标列表  插入到项目监控中的指标表中
            List<InfoProjectTargetIndex> infoProjectTargetIndices = infoProjectTargetIndexMapper.selectInfoProjectTargetIndexList(infoProjectTargetIndex);
            for (InfoProjectTargetIndex projectTargetIndex : infoProjectTargetIndices) {
                InfoProjectMonitorIndex infoProjectMonitorIndex = new InfoProjectMonitorIndex();
                BeanUtils.copyProperties(projectTargetIndex, infoProjectMonitorIndex);
                infoProjectMonitorIndex.setTargetThree(projectTargetIndex.getTargetName());
                //此处的项目id为监控表中的项目id
                infoProjectMonitorIndex.setProjectMonitorRelationId(infoProjectMonitorRelation.getId());
                infoProjectMonitorIndexMapper.insertInfoProjectMonitorIndex(infoProjectMonitorIndex);
            }
        }
        return i;
    }

    @Override
    public void reportProjectMonitorBatch(List<Long> projectIds,String monitorTime,String remark){
        for(Long projectId:projectIds){
            ProjectMonitorVo projectMonitorVo =new ProjectMonitorVo();
            projectMonitorVo.setProjectId(projectId);
            projectMonitorVo.setMonitorTime(monitorTime);
            projectMonitorVo.setRemark(remark);
            this.reportProjectMonitor(projectMonitorVo);
        }
    }

    @Override
    public InfoProjectMonitorDetailVo getProjectMonitorDetail(Long projectMonitorId) {
        //1.查询项目信息
        InfoProjectMonitorDetailVo detailVo = infoProjectMonitorMapper.getProjectMonitorDetailById(projectMonitorId);
        if (detailVo == null) {
            throw new RuntimeException("项目不存在");
        }
        //2.查询最后一期的项目资金 执行率 若不存在  则为 0

        InfoProjectMonitorFunds projectMonitorFunds = infoProjectMonitorRelationMapper.selectLaseMonitorTimeFunds(projectMonitorId);

        if (null == projectMonitorFunds) {
            //不存在时 说明没有执行期的数据
            //根据主项目id查询资金
            projectMonitorFunds = infoProjectMonitorMapper.selectInfoProkectMontorFunds(projectMonitorId);
        }
        detailVo.setInfoProjectMonitorFunds(projectMonitorFunds);
        return detailVo;
    }

    @Override
    public InfoProjectMonitor selectInfoProjectMonitorByIdss(Long id) {
        return infoProjectMonitorMapper.selectInfoProjectMonitorByIdss(id);
    }
}
